// countdown.txt
// Modified basicnpc.txt, which emulates Nociduas from A4. The creature
//  waits x number of turns "chanting", before seriously buffing itself
//  and rushing the party. While "chanting", the creature's turn always
//  ends early, before it can do anything.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. When the chanting ends, this flag is set
//     to one. When the creature is killed, it's set to two. Cannot be left
//     blank.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - How many turns the chanting goes.

begincreaturescript;

variables;

short i,target;
short chanting = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);

	if(get_memory_cell(4) != 0)
		chanting = get_memory_cell(4);

	if(get_flag(get_memory_cell(1), get_memory_cell(2)) == 0) {
		alter_stat(ME,9,15);
		alter_stat(ME,20,15);
		}
	else if(get_flag(get_memory_cell(1), get_memory_cell(2)) == 1) {
		alter_stat(ME,12,10);
		alter_stat(ME,28,5);
		alter_stat(ME,0,6);
		alter_stat(ME,3,5);
		set_strategy(ME,0);
		}

	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),2);
break;

beginstate START_STATE; 

	// if I'm done chanting, buff me up
	if((chanting == 0) && (get_flag(get_memory_cell(1), get_memory_cell(2)) == 0) && (get_attitude(ME) == 10)) {
		set_flag(get_memory_cell(1), get_memory_cell(2), 1);
		reset_dialog();
		add_dialog_str(0,"The priest stops chanting. This is slightly more alarming than the chanting itself.",0);
		add_dialog_str(1,"A bright violet halo of energy surrounds the ghost, and it acknowledges your presence for the first time. The occasional crackle of discharging energy are starting to worry you.",0);
		run_dialog(1);
		put_sparkles_on_char(ME,6,20);
		run_animation_sound(62);
		alter_stat(ME,12,10);
		alter_stat(ME,28,5);
		alter_stat(ME,0,6);
		alter_stat(ME,3,5);
		alter_stat(ME,9,-10);
		alter_stat(ME,20,-20);
		change_char_health(ME,200);
		set_strategy(ME,0);
		}

	// if I'm still chanting, end my turn
	if((chanting > 0) && (get_flag(get_memory_cell(1), get_memory_cell(2)) == 0) && (get_attitude(ME) == 10)) {
		chanting = chanting - 1;
		if(get_health(ME) == get_max_health(ME))
			print_str_color("The priest continues to chant.",2);
		else {
			print_str_color("The priest continues to chant, healing some damage.",2);
			change_char_health(ME, 50);
			}
		end_combat_turn();
		}

	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			do_attack();
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;